package com.loe.logger

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.logger_test_fragment.*

class LoggerTestFragment : Fragment()
{
    protected lateinit var inflater: LayoutInflater

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        this.inflater = inflater
        return inflater.inflate(R.layout.logger_test_fragment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)

        initList()
        initInfo()
    }

    private lateinit var adapter: LoggerTestAdapter

    private fun initList()
    {
        adapter = LoggerTestAdapter(activity!!)
        listView.adapter = adapter
    }

    private fun initInfo()
    {
        buttonToHttpTest.setOnClickListener()
        {
            LoeLogger.onHttpTestPage?.invoke(activity!!)
        }

        buttonAdd.setOnClickListener()
        {
            LoggerAddTestDialog(activity!!).show()
            { name, url ->
                adapter.addBean(name, url)
            }
        }

        listView.setOnItemClickListener()
        { adapterView, view, i, l ->
            // 打开页面
            val bean = adapter.getItem(i) as LoggerTestAdapter.Bean
            LoeLogger.onOpenPage?.invoke(activity!!, bean.url)
        }
    }
}